<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2017/2/26
 * Time: 16:37
 */
namespace app\admin\controller;

use think\Loader;

class Index extends Base
{
    /**
     * 后台首页
     * by kuddy
     * @return mixed
     */
    public function index()
    {
        $title = '控制中心';
        $this->assign('title',$title);
        return $this->fetch();
    }

    /**
     * 登录页面
     * by kuddy
     * @return mixed
     */
    public function login()
    {
        //检查登录状态
        if (!empty(session('admin'))) {
            $this->redirect(url('/admin'));
        }
        //提交表单处理
        if ($this->request->isPost()) {
            $admin = Loader::model('login', 'logic');
            $res = $admin->check(input('post.'));
            if (true === $res) {
                $admin = $admin->getAdmin();
                session('admin', $admin['id']);
                $this->success('欢迎您，管理员：' . $admin['nickname'], '/admin');
            }
            //登录不成功更新表单token
            $this->request->token();
            if (false === $res) {
                $this->error('用户名或密码错误');
            }
            $this->error($res);
        }
        $title = '后台登录';
        $this->assign('title',$title);
        return $this->fetch();
    }

    public function quit()
    {
        session('admin', null);
        $this->success('您已成功退出系统',url('/admin/login'));
    }
}